package com.seari.custody.controller;

import com.seari.custody.bean.TechnicalAuditOpinion;
import com.seari.custody.bean.TechnicalHistory;
import com.seari.custody.util.JwtTokenUtils;
import com.seari.custody.constant.MessageConstant;
import com.seari.custody.pojo.MessageBean;
import com.seari.custody.service.TechnicalAdviceService;
import com.seari.custody.service.TechnicalAuditOpinionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static com.seari.custody.util.JwtTokenUtils.TOKEN_HEADER;

/**
 * 技审对接报审接口
 */
@Slf4j
@RestController
public class TechnicalAuditOpinionController {

    @Autowired
    private TechnicalAuditOpinionService technicalAuditOpinionService;

    @Autowired
    private TechnicalAdviceService technicalAdviceService;

    /**
     * 3.2.4.报审审核技审草稿盖章通过下发技审
     */
    @RequestMapping(value = "/technicalAuditOpinion/passTechnical")
    public MessageBean passTechnical(@Valid @RequestBody TechnicalAuditOpinion technicalAuditOpinion) {
        MessageBean messageBean = new MessageBean();
        try{
            Map<String, String> map = technicalAuditOpinionService.passTechnical(technicalAuditOpinion);
            String code=map.get("code");
            String message=map.get("message");
            messageBean.setCode(code);
            messageBean.setMessage(message);
        }catch (Exception e) {
            e.printStackTrace();
            messageBean.setCode(MessageConstant.FAIL);
            messageBean.setMessage("系统异常");
        }
        return messageBean;
    }

    /**
     * 3.2.5.	报审审核技审草稿不通过下发技审
     */
    @RequestMapping(value = "/technicalAuditOpinion/sendTechnical")
    public MessageBean sendTechnical(@Valid @RequestBody TechnicalAuditOpinion technicalAuditOpinion) {
        MessageBean messageBean = new MessageBean();
        try{
            Map<String, String> map = technicalAuditOpinionService.sendTechnical(technicalAuditOpinion);
            String code=map.get("code");
            String message=map.get("message");
            messageBean.setCode(code);
            messageBean.setMessage(message);
        }catch (Exception e) {
            e.printStackTrace();
            messageBean.setCode(MessageConstant.FAIL);
            messageBean.setMessage("系统异常");
        }
        return messageBean;
    }

    /**
     * 3.2.7.	专项保护方案（包含修改后）下发技审
     */
    @RequestMapping(value = "/technicalAuditOpinion/updateTechnical")
    public MessageBean updateTechnical(@RequestBody Map<String, String> map1) {
        MessageBean messageBean = new MessageBean();
        try{
            Map<String, String> map = technicalAuditOpinionService.updateTechnical(map1);
            String code=map.get("code");
            String message=map.get("message");
            messageBean.setCode(code);
            messageBean.setMessage(message);
        }catch (Exception e) {
            e.printStackTrace();
            messageBean.setCode(MessageConstant.FAIL);
            messageBean.setMessage("系统异常");
        }
        return messageBean;
    }


    /**
     *
     * 3.2.8.	MMS专项保护方案退回报审
     */
    @RequestMapping("/technicalAuditOpinion/sendBackSpecial")
    public MessageBean sendBackSpecial(@RequestBody Map<String, String> map){
        MessageBean messageBean = new MessageBean();
        try{
            Map<String, String> map1 = technicalAuditOpinionService.sendBackSpecial(map);
            String code=map1.get("code");
            String message=map1.get("message");
            messageBean.setCode(code);
            messageBean.setMessage(message);
        }catch (Exception e) {
            e.printStackTrace();
            messageBean.setCode(MessageConstant.FAIL);
            messageBean.setMessage("系统异常");
        }
        return messageBean;
    }

    /**
     * 代办统计
     * @param
     * @return
     */
    @RequestMapping("/technicalAuditOpinion/commissionStatistics")
    public MessageBean commissionStatistics(@RequestHeader(value = TOKEN_HEADER, defaultValue = "") String token, @RequestBody Map<String, String> map){
        MessageBean messageBean = new MessageBean();
        //token为空的情况下
        if (StringUtils.isNotEmpty(token) == false) {
            messageBean.setType(MessageConstant.FAIL_TYPE);
            messageBean.setCode(MessageConstant.FAIL);
            messageBean.setMessage("token为空生成失败");
            return messageBean;
        }
        String userNumber = JwtTokenUtils.getUserNumber(token.substring(JwtTokenUtils.TOKEN_PREFIX.length()));
        try{
            map.put("manager_number",userNumber);
            Map<String, Integer> map1 = technicalAuditOpinionService.orderVerdict(map);
            messageBean.setType(MessageConstant.SUCCESS_TYPE);
            messageBean.setCode(MessageConstant.SUCCESS);
            messageBean.setMessage("生成审核意见模板成功");
            messageBean.setData(map1);
        }catch (Exception e) {
            e.printStackTrace();
            messageBean.setCode(MessageConstant.FAIL);
            messageBean.setMessage("系统异常");
        }
        return messageBean;
    }

    /**
     * MMS-技审草稿上传
     *
     */
    @RequestMapping("/technicalAuditOpinion/scratchFile")
    public MessageBean scratchFile(@Valid @RequestBody TechnicalAuditOpinion technicalAuditOpinion) {
        MessageBean messageBean = new MessageBean();
        TechnicalHistory technicalHistory = new TechnicalHistory();
        Date date = new Date();
        String no=technicalAuditOpinion.getNo();
        // 得到no值获取uuid
        Map<String, String> map=new HashMap<>();
        map.put("no",no);
        map.put("type",technicalAuditOpinion.getType());
        Map<String, Object> stringObjectMap = technicalAdviceService.engineeringInformation(map);
        // uuid
        String uuid= (String) stringObjectMap.get("uuid");
        // 流程状态
        String flow_status= (String) stringObjectMap.get("flow_status");
        if(flow_status.equals("1")){
            messageBean.setType(MessageConstant.FAIL_TYPE);
            messageBean.setCode(MessageConstant.FAIL);
            messageBean.setMessage("技审未生成草稿文件，不可直接上传");
            return messageBean;
        }else if(!flow_status.equals("3")){
            messageBean.setType(MessageConstant.FAIL_TYPE);
            messageBean.setCode(MessageConstant.FAIL);
            messageBean.setMessage("技审待办状态有误，不可直接上传");
            return messageBean;
        }
        // 在实体类插入uuid
        technicalAuditOpinion.setUuid(uuid);
        try {
           // 上传非docx文件格式
            Map<String, String> mken = technicalAuditOpinionService.uploadstatus(technicalAuditOpinion);
            String code = mken.get("code");
            String message = mken.get("message");
            messageBean.setCode(code);
            messageBean.setMessage(message);
            technicalHistory.setCreateTime(date);
            technicalHistory.setOperationalType("1");
            technicalHistory.setOperationalContext("专项草稿审核");
            technicalHistory.setOperationalFlow("4");
            technicalHistory.setOperationalOperator(technicalAuditOpinion.getPersonNumber());
            technicalHistory.setOperationalState("1");
            technicalHistory.setOperationalProject(uuid);
            technicalAdviceService.serverTechnicalHistory(technicalHistory);
        } catch (Exception e) {
            technicalHistory.setCreateTime(date);
            technicalHistory.setOperationalType("1");
            technicalHistory.setOperationalContext("专项草稿审核");
            technicalHistory.setOperationalFlow("4");
            technicalHistory.setOperationalOperator(technicalAuditOpinion.getPersonNumber());
            technicalHistory.setOperationalState("0");
            technicalHistory.setOperationalProject(uuid);
            technicalAdviceService.serverTechnicalHistory(technicalHistory);
            e.printStackTrace();
            messageBean.setType(MessageConstant.FAIL_TYPE);
            messageBean.setCode(MessageConstant.FAIL);
            messageBean.setMessage("上传技审文件失败");
        }
        return messageBean;
    }

/**
 * 待办统计接口
 */
@RequestMapping("/technicalAuditOpinion/backlog")
public MessageBean saverBacklog(@RequestBody Map<String, String> map) {
    MessageBean messageBean = new MessageBean();
    try{
        Map<String, Object> stringObjectMap = technicalAdviceService.queryBacklog(map);
        messageBean.setType(MessageConstant.SUCCESS_TYPE);
        messageBean.setCode(MessageConstant.SUCCESS);
        messageBean.setMessage("生成待办列表成功");
        messageBean.setData(stringObjectMap);
    }catch (Exception e) {
        e.printStackTrace();
        messageBean.setCode(MessageConstant.FAIL);
        messageBean.setMessage("系统异常");
    }
    return messageBean;
}

/**
 * 获取技审草稿pdf编号
 */
@RequestMapping(value = "/technicalAuditOpinion/documentQuery")
public MessageBean documentQuery( @RequestBody Map<String, String> map) {
    MessageBean messageBean = new MessageBean();
    try{
        Map<String, Object> selectPdf = technicalAuditOpinionService.selectStampPdf(map);
        String code= (String) selectPdf.get("code");
        String message= (String) selectPdf.get("message");
        messageBean.setCode(code);
        messageBean.setMessage(message);
        messageBean.setData(selectPdf.get("data"));
    }catch (Exception e) {
        e.printStackTrace();
        messageBean.setCode(MessageConstant.FAIL);
        messageBean.setMessage("系统异常");
    }
    return messageBean;
}


}
